#include <Windows.h>
#include "CTimer.h"

CTimer::CTimer(void)
{
	m_bIsRunning = false;
	m_dElapsedTime = 0.0;

	QueryPerformanceFrequency( (LARGE_INTEGER*)&m_llFrequency );

	QueryPerformanceCounter( (LARGE_INTEGER*)&m_llStartTick );

	m_nFrameCount		= 0;	
	m_nFPS				= 0;			
	m_dwFPSTimeStamp	= GetTickCount();
}

void CTimer::Start(void)
{
	if( !m_bIsRunning )
	{
		m_bIsRunning = true;

		QueryPerformanceCounter( (LARGE_INTEGER*)&m_llStartTick );
	}
}

void CTimer::Stop(void)
{
	if( m_bIsRunning )
	{
		m_bIsRunning = false;

		m_dElapsedTime = GetElapsedTime();
	}
}

void CTimer::Reset(void)
{
	m_dElapsedTime = 0.0;
	QueryPerformanceCounter( (LARGE_INTEGER*)&m_llStartTick );	
}

double CTimer::GetElapsedTime(void)
{
	if( m_bIsRunning )
	{
		LONGLONG	llStopTick;
		QueryPerformanceCounter( (LARGE_INTEGER*)&llStopTick );

		m_dElapsedTime = (double)(llStopTick - m_llStartTick) / (double)m_llFrequency;
	}
	return m_dElapsedTime;
}

int CTimer::FPS(void)
{
	///////////////////////////////////
	// Count FPS
	m_nFrameCount++;

	// check if 1 second has passed
	if( GetTickCount() - m_dwFPSTimeStamp > 1000 )
	{
		// remember the frame count
		m_nFPS = m_nFrameCount;
		// reset the current frame count
		m_nFrameCount = 0;
		// get a new time stamp
		m_dwFPSTimeStamp = GetTickCount();
	}

	return m_nFPS;
}
